package demo.maven;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class WelcomeSelvlet
 */
@WebServlet("/WelcomeSelvlet")
public class WelcomeSelvlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static Logger logger = Logger.getLogger("chapter07");

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public WelcomeSelvlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String strcmd = "cmd /c start  D:/soft/apache-tomcat-7.0.86/bin/shutdown.bat"; // 执行bat命令
		runCmd(strcmd);

		response.getWriter().println("Hello, firebug! restart");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public static void runCmd(String strcmd) {
		//
		Runtime rt = Runtime.getRuntime(); //

		try {
			Process	ps = rt.exec(strcmd);
			ps.waitFor();
			int i = ps.exitValue(); //
			if (i == 0) {
				logger.log(Level.INFO, "运行成功");
			} else {
				logger.log(Level.WARNING, "运行失败");
			}
			ps.destroy(); //
		} catch (IOException e) {
			logger.log(Level.WARNING, "执行异常", e);
		} catch (InterruptedException e) {
			logger.log(Level.WARNING, "等待异常", e);
		}

		
	}

}